package cn.fyl.system.service.impl;

import cn.fyl.basic.service.impl.BaseServiceImpl;
import cn.fyl.system.domain.Menu;
import cn.fyl.system.domain.Role;
import cn.fyl.system.mapper.RoleMapper;
import cn.fyl.system.service.IMenuService;
import cn.fyl.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void save(Role role) {
        //保存角色信息
        roleMapper.save(role);
        //操作相关信息：中间表信息
        roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        roleMapper.saveRolePermission(role.getId(),role.getPermissions());
    }

    @Override
    public void remove(Long id){

        //删除中间表：t_role_permission
        roleMapper.removeRolePermissionByRoldId(id);
        //删除中间表：t_role_menu
        roleMapper.removeRoleMenuByRoldId(id);
        // 删除emp表
        
        //删除角色
        roleMapper.remove(id);
    }

    @Override
    public void update(Role role) {
        roleMapper.update(role);
        roleMapper.removeRoleMenuByRoldId(role.getId());
        roleMapper.removeRolePermissionByRoldId(role.getId());
        roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        roleMapper.saveRolePermission(role.getId(),role.getPermissions());
    }
}
